/*
 * RelaisCard.h
 *
 *  Created on: 13.05.2009
 *      Author: michael
 */

#ifndef RELAISCARD_H_
#define RELAISCARD_H_

#define FALSE_REL_NR	"Ungültige Relaisnummer!\n"
#define FALSE_CARD_NR	"Ungültige Relaiskartennummer!\n"

#define NR_OF_RELAIS	8
#define MAX_VALUE		((1 << NR_OF_RELAIS) - 1)
#define FIRST_RELAIS	1

#include <sstream>
#include <stdexcept>
#include <vector>
#include <iostream>

using namespace std;

class RelaisCardModel {
private:
	vector<unsigned char> cards;

public:
	RelaisCardModel() {};
	~RelaisCardModel() {};

	unsigned char getCard( unsigned char id ) throw (exception){
		if (id > cards.size())
			throw (out_of_range(FALSE_CARD_NR));
		return cards[id - 1];
	};

	void setCard( unsigned char card, unsigned char value) throw (exception){
		if (card > cards.size())
			throw (out_of_range(FALSE_CARD_NR));
		cards[card - 1] = value;
	};

	void addCard( void ){ cards.push_back(0); };

	bool isSet(unsigned char card, unsigned char RelaisNr) throw (exception){
		return (getCard(card) & getRelaisValue(RelaisNr));
	};

	string toString( void ){
		stringstream s;
		s << endl <<"      Auflistung der Karten mit ihren Zuständen:" << endl;
		s << "         +-1-+-2-+-3-+-4-+-5-+-6-+-7-+-8-+" << endl;
		for(unsigned char card = 1; card <= cards.size(); card++){
			s << "Karte " << (int)card << ": ";
			for(unsigned char rel = FIRST_RELAIS; rel <= NR_OF_RELAIS; rel++)
				try {
					s << ((isSet(card, rel)) ? "| x " : "|   ");
				} catch (exception e){
					cerr << "Fehler in der toString()-Methode des RelaisCardModel.h" << endl << e.what();
				}
				s << "| Value: " << (int)getCard(card) << endl;
		}
		s << "         +---+---+---+---+---+---+---+---+" << endl;
		return s.str();
	}

	bool isEmpty( void ) { return cards.size() == 0; };
	unsigned char size( void ) { return cards.size(); };


	unsigned char getRelaisValue(unsigned char RelaisNr) throw (exception){
		if (RelaisNr > NR_OF_RELAIS || RelaisNr < FIRST_RELAIS)
			throw out_of_range(FALSE_REL_NR);
		return (1 << (RelaisNr - 1));
	}

	unsigned char getMaxValue( void ){ return MAX_VALUE; }
};

#endif /* RELAISCARD_H_ */
